package com.example.milimili;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.milimili.bean.NewsInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {
    private List<NewsInfo> newsInfoList;
    private ListView listView;
    private TextView newsText;
    private ImageView myImage;
    private ImageButton backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(this);
        newsInfoList=mySQLiteOpenHelper.queryAllInfo(2);
        initView();
    }

    private void initView() {
        listView=findViewById(R.id.listViewCollect);
        MyAdapter myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String path=newsInfoList.get(i).getPath();
                int position=newsInfoList.get(i).getPosition();
                WebAdapter webAdapter=new WebAdapter(path,position);
                HashMap<String,Object> map=webAdapter.getMap();
                Intent intent=new Intent(view.getContext(),NewsActivity.class);
                final SerializableMap myMap=new SerializableMap();
                myMap.setMap(map);//将map数据添加到封装的myMap中
                Bundle bundle=new Bundle();
                bundle.putSerializable("map", myMap);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        backBtn=findViewById(R.id.backButton);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(HistoryActivity.this,MainActivity.class);
                HistoryActivity.this.startActivity(intent);
            }
        });
    }

    class WebAdapter{
        private String path,json;
        private int position;
        private HashMap<String,Object> map;

        public HashMap<String, Object> getMap() {
            return map;
        }

        public WebAdapter(String path, int position) {
            this.path = path;
            this.position = position;
            webThread.start();
            try {
                webThread.join();
                this.map=getJson(json,this.position);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        Thread webThread = new Thread(new Runnable() {
            @Override
            public void run() {
                getWeb(path);
            }
        });

        public void getWeb(String path){
            Log.e("data",path);
            try {
                json=HtmlService.getHtml(path);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.e("data",json);
        }

        private HashMap<String,Object> getJson(String json,int position) {
            HashMap<String, Object> map=null;
            try {
                JSONObject rootObject = new JSONObject(json);
                JSONArray dataArray=rootObject.getJSONArray("data");
                JSONObject newsObject = dataArray.getJSONObject(position);
                String imageAll=newsObject.getString("image");
                String imageTmp=imageAll.substring(1,imageAll.length()-1);
                String video= newsObject.getString("video");
                String title= newsObject.getString("title");
                String content=newsObject.getString("content");
                String organizations= newsObject.getString("organizations");
                map = new HashMap<>();
                map.put("image",imageTmp);
                map.put("video", video);
                map.put("title", title);
                map.put("content",content);
                map.put("organization", video);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return map;
        }
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return newsInfoList.size();
        }

        @Override
        public Object getItem(int i) {
            String str=newsInfoList.get(i).getTitle();
            return str;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View itemView=View.inflate(HistoryActivity.this,R.layout.itemlayout,null);
            newsText=itemView.findViewById(R.id.name);
            myImage=itemView.findViewById(R.id.picture);
            newsText.setText(newsInfoList.get(i).getTitle());
            String imageAll=newsInfoList.get(i).getImage();
            String[] imageArray=imageAll.split(", ");
            Glide.with(HistoryActivity.this).load(imageArray[0]).into(myImage);
            newsText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String path=newsInfoList.get(i).getPath();
                    int position=newsInfoList.get(i).getPosition();
                    WebAdapter webAdapter=new WebAdapter(path,position);
                    HashMap<String,Object> map=webAdapter.getMap();
                    Intent intent=new Intent(view.getContext(),NewsActivity.class);
                    final SerializableMap myMap=new SerializableMap();
                    myMap.setMap(map);//将map数据添加到封装的myMap中
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("map", myMap);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
            return itemView;
        }
    }
}